#include <iostream>
using namespace std;

const int kMaxN = 2e5 + 10;
const int kInf = 1e9;

int f[kMaxN], max_f[kMaxN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n;

    cin >> n;
    fill(f, f + n + 1, -kInf);
    fill(max_f, max_f + n + 1, -kInf);
    f[0] = 0;
    for (int i = 1; i <= n; i++) {
      int a;

      cin >> a;
      f[i] = max(f[i - 1], max_f[a] + i);
      max_f[a] = max(max_f[a], f[i - 1] - (i - 1));
    }
    cout << f[n] << "\n";
  }
  return 0;
}